package cn.kshost.zqyg.manager.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.kshost.zqyg.manager.exception.ZhongQiaoException;
import cn.kshost.zqyg.manager.mapper.SysUserMapper;
import cn.kshost.zqyg.manager.service.SysUserService;
import cn.kshost.zqyg.model.dto.h5.UserLoginDto;
import cn.kshost.zqyg.model.dto.system.AssginRoleDto;
import cn.kshost.zqyg.model.dto.system.LoginDto;
import cn.kshost.zqyg.model.dto.system.SysUserDto;
import cn.kshost.zqyg.model.entity.system.SysUser;
import cn.kshost.zqyg.model.vo.common.ResultCodeEnum;
import cn.kshost.zqyg.model.vo.h5.UserInfoVo;
import cn.kshost.zqyg.model.vo.system.LoginVo;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl implements SysUserService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public LoginVo login(LoginDto loginDto) {
        //获取验证码的key
        String codeKey = loginDto.getCodeKey();
        //获取验证验证码的值
        String captchaValue = loginDto.getCaptcha();
        //通过key获取验证码 前缀需要user:login:validatecode:
        String realCaptureVale = redisTemplate.opsForValue().get("user:login:validatecode:" + codeKey);
        //如果获取的验证码是空或者与输入验证码不相等 抛出验证码错误异常
        if (StrUtil.isEmpty(realCaptureVale)||!StrUtil.equalsIgnoreCase(realCaptureVale,captchaValue)) {
            throw new ZhongQiaoException(ResultCodeEnum.VALIDATECODE_ERROR);
        }
        //验证码正确后 删除验证码
        redisTemplate.delete("user:login:validatecode:" + codeKey);
        SysUser sysUser = sysUserMapper.selectSysUserByUsername(loginDto.getUserName());
        if (sysUser == null) {
            throw new ZhongQiaoException(ResultCodeEnum.LOGIN_ERROR);
        }
        String password = loginDto.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //存入redis
        if (md5Password.equals(sysUser.getPassword())) {
            String token = UUID.randomUUID().toString().replace("-", "");
            redisTemplate.opsForValue().set("user:login:" + token, JSON.toJSONString(sysUser),5, TimeUnit.HOURS);
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(token);
            loginVo.setRefresh_token("");
            return loginVo;
        }else {throw new ZhongQiaoException(ResultCodeEnum.LOGIN_ERROR);
        }
    }

    @Override
    public UserInfoVo getUserInfo(String token) {
        UserInfoVo userInfoVo = JSON.parseObject(redisTemplate.opsForValue().get("user:login:" + token), UserInfoVo.class);
        return userInfoVo;
    }

    @Override
    public void logout(String token) {
        redisTemplate.delete("user:login:" + token);
    }

    @Override
    public PageInfo<SysUser> findByPage(Integer pageNum, Integer pageSize, SysUserDto sysUserDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysUser> sysUserList =  sysUserMapper.selectUserByPage(sysUserDto);
        PageInfo<SysUser> sysUserPageInfo = new PageInfo<>(sysUserList);
        return sysUserPageInfo;
    }

    @Override
    public void addSysUser(SysUser sysUser) {
        sysUserMapper.insertSysUser(sysUser);
    }

    @Override
    public void modifySysSUser(SysUser sysUser) {
//        //查询用户名是否已经存在
//        SysUser selectSysUserByUsername = sysUserMapper.selectSysUserByUsername(sysUser.getUserName());
//        if (null!=selectSysUserByUsername) {
//            throw new ZhongQiaoException(ResultCodeEnum.USER_NAME_IS_EXISTS);
//        }
        //如果前端设置了密码做加密处理
        if (!StrUtil.isEmpty(sysUser.getPassword())) {
            sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes()));
        }

        sysUserMapper.updateSysUser(sysUser);


    }

    @Override
    public void deleteSysUser(SysUser sysUser) {
        sysUserMapper.deleteSysUser(sysUser);

    }

}
